package com.seveneight.baijiesteward.ui.first.fragment;

import android.annotation.SuppressLint;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.TeamAdapter;
import com.seveneight.baijiesteward.base.BaseFragmentBack;
import com.seveneight.baijiesteward.base.TeamBean;
import com.seveneight.baijiesteward.ui.first.contract.TeamListContract;
import com.seveneight.baijiesteward.ui.first.presenter.TeamListPresenter;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by user1 on 2018/4/4.
 */

@SuppressLint("ValidFragment")
public class MyTeamListFragment extends BaseFragmentBack implements TeamListContract.TeamListView {

    TeamListContract.TeamListPresenter teamListPresenter;
    private LRecyclerView recyclerview;
    private TeamAdapter teamAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int page = 1;
    private int type;

    public MyTeamListFragment(int type) {
        this.type = type;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initView() {
        recyclerview = findView(R.id.recyclerview);
        setSwipeBackEnable(false);
        RecyclerviewInitUtil.initRefreshLinearReccycler(mContext, recyclerview,R.dimen.divider_1dp);
        teamAdapter = new TeamAdapter(mContext,type);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(teamAdapter);
        recyclerview.setAdapter(lRecyclerViewAdapter);
    }

    @Override
    protected void initData() {
        teamListPresenter = new TeamListPresenter(this);
        getData();
    }

    private void getData() {
        teamListPresenter.getTeamList(mContext, jsonFormat.teamList(page, type));
    }

    @Override
    protected void setListener() {
        recyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                teamAdapter.clear();
                getData();
            }
        });

        recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    @Override
    public void rebackTeamData(List<TeamBean> data) {
        recyclerview.refreshComplete(data.size());
        teamAdapter.addAll(data);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }
}
